package jiubeirobot.com.robotshop.ui.activity;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import butterknife.BindView;
import butterknife.OnClick;
import jiubeirobot.com.robotshop.R;
import jiubeirobot.com.robotshop.base.Activitys;
import jiubeirobot.com.robotshop.base.BaseActivity;
import jiubeirobot.com.robotshop.base.BaseApplication;
import jiubeirobot.com.robotshop.base.BaseBean;
import jiubeirobot.com.robotshop.base.BaseData;
import jiubeirobot.com.robotshop.bean.ObjBean;
import jiubeirobot.com.robotshop.bean.RobotInfoBean;
import jiubeirobot.com.robotshop.common.view.AddressSelectedView;
import jiubeirobot.com.robotshop.data.net.APIManager;
import jiubeirobot.com.robotshop.data.net.Flags;
import jiubeirobot.com.robotshop.utils.Arith;
import jiubeirobot.com.robotshop.utils.DataUtil;
import jiubeirobot.com.robotshop.utils.L;
import jiubeirobot.com.robotshop.utils.RegexUtils;
import jiubeirobot.com.robotshop.utils.SharedPreferencesUtils;
import jiubeirobot.com.robotshop.utils.TS;

/**
 * @author TheJoker丶豪
 *         这是初始化APP的时候需要设置资料的activity
 * @date 创建时间:2017/8/18
 */

public class SetRobotActivity extends BaseActivity {
    //经度
    @BindView(R.id.et_longitude)
    EditText etlongitude;
    //纬度
    @BindView(R.id.et_latitude)
    EditText etlatitude;
    //地址选择器
    AddressSelectedView llytaddress;
    //账户名
    @BindView(R.id.et_number)
    EditText etnumber;
    @BindView(R.id.btn_setfinish)
    Button btnSetfinish;
    @BindView(R.id.llyt_address_item)
    LinearLayout llytAddressItem;
    @BindView(R.id.et_consignee_details_address)
    EditText etdetails;
    @BindView(R.id.et_password)
    EditText etpwd;


    String longitude;
    String latitude;
    String area_id;
    String username;

    private static final int DECIMAL_DIGITS = 2;//小数的位数

    @Override
    public int pageLayout() {
        return R.layout.activity_set_robot;
    }

    @Override
    public void initTitle() {
        boolean isFirst = SharedPreferencesUtils.getboolean(SetRobotActivity.this, "isfrist");
        L.d(isFirst);
        if (isFirst) {
            Activitys.toMain(SetRobotActivity.this);
            finish();
        } else {
            Activitys.toSetAct(SetRobotActivity.this);
        }
    }

    @Override
    public void initView() {
        setPoint(etlongitude);
        setPoint(etlatitude);

        llytaddress = (AddressSelectedView) findViewById(R.id.llyt_address);
        llytaddress.setCancelClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                llytaddress.setVisibility(View.GONE);
            }
        });
        llytaddress.setOptionsSelectListener(new AddressSelectedView.OptionsSelectListener() {
            @Override
            public void onOptionsSelect(String province, String city, String bean, String id, View v) {
                llytaddress.setVisibility(View.GONE);
                area_id = id;
                Log.d("print", "onOptionsSelect: 选中的id是" + id);
            }
        });
    }

    @Override
    public void onBackPressed() {

    }

    @Override
    public void onSuccess(BaseBean bean, int flag) {
        switch (flag) {
            case Flags.FLAG_PUT_ROBOT_INFO:
                Log.d("print", "onSuccess: " + bean);
                BaseBean<ObjBean<RobotInfoBean>> infobean = bean;
                RobotInfoBean robotInfoBean = infobean.getData().getObj();
                TS.showToCenter(infobean.getMessage());
                SharedPreferencesUtils.putString(SetRobotActivity.this,"delivery_id",robotInfoBean.getDelivery_id());
                Activitys.toMain(SetRobotActivity.this);
                finish();
                break;
            default:
                break;
        }
        dismissHUD();
    }

    String area_location;
    String etaddressdetails;


    @OnClick({R.id.btn_setfinish, R.id.llyt_address_item})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_setfinish:
                username = etnumber.getText().toString();
                String password = etpwd.getText().toString();
                longitude = etlongitude.getText().toString();
                latitude = etlatitude.getText().toString();

                latitude = Arith.get2Decimal(latitude);
                longitude = Arith.get2Decimal(longitude);

                if (!TextUtils.isEmpty(password)) {
                    DataUtil.getInstance().saveMobilePWD(SetRobotActivity.this, password);
                }
                if (!TextUtils.isEmpty(username)) {
                    if (RegexUtils.checkMobile(username)) {
                        SharedPreferencesUtils.putBoolean(SetRobotActivity.this, "isfrist", true);
                        APIManager.getInstance().putRobotInfoToServer(SetRobotActivity.this, username, area_id, etaddressdetails, area_location, BaseData.getBaseData().getROBOT_SIGN(BaseApplication.getInstance()));
                    } else {
                        TS.showToCenter(getString(R.string.mobile_number_error));
                    }
                } else {
                    TS.showToCenter(getString(R.string.mobile_number_error));
                }
                if (!TextUtils.isEmpty(username) && RegexUtils.checkMobile(username)) {
                    DataUtil.getInstance().saveMobileNumber(SetRobotActivity.this, username);
                }
                break;
            case R.id.llyt_address_item:
                if (llytaddress.getVisibility() == View.VISIBLE) {
                    llytaddress.setVisibility(View.GONE);
                } else {
                    llytaddress.setVisibility(View.VISIBLE);
                }
                break;
            default:
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


    public static void setPoint(final EditText editText) {
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.toString().contains(".")) {
                    if (s.length() - 1 - s.toString().indexOf(".") > DECIMAL_DIGITS) {
                        s = s.toString().subSequence(0,
                                s.toString().indexOf(".") + DECIMAL_DIGITS + 1);
                        editText.setText(s);
                        editText.setSelection(s.length());
                    }
                }
                if (s.toString().trim().substring(0).equals(".")) {
                    s = "0" + s;
                    editText.setText(s);
                    editText.setSelection(2);
                }
                if (s.toString().startsWith("0")
                        && s.toString().trim().length() > 1) {
                    if (!s.toString().substring(1, 2).equals(".")) {
                        editText.setText(s.subSequence(0, 1));
                        editText.setSelection(1);
                        return;
                    }
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
    }
}
